/**
 * jQuery plugin for show default values in input elements.
 * 
 * Examples:
 * 	$('#email').defaultValue('Input your email');
 *  $('#www').defaultValue('Input your site here...', {'inactiveColor': #333333,
 *  												   'activeColor': #00FF00});
 * 
 * http://code.google.com/p/jquerydef/
 * nix.n0uk@gmail.com
 * License: GNU General Public License v3
 */

(function ($) {
	$.fn.defaultValue = function(defaultText, options) {

		if (!options) {
			options = {};
		}

		var $input = $(this);
		
		options.inactiveColor = options.inactiveColor || '#999999';
		options.activeColor = options.activeColor || '#000000';
		
		/**
		 * Remove default value on focus
		 */
		$input.focus(function () {
			if ($.trim($input.val()) == defaultText)
			{
				$input.css('color', options.activeColor);
				$input.val('');
			}
		});
		
		/**
		 * Restore default value on blur with empty field
		 */
		$input.blur(function () {
			if ($.trim($input.val()) == '') {
				$input.css('color', options.inactiveColor);
				$input.val(defaultText);
			}
		});
		
		/**
		 * Initialize default value on startup
		 */
		$(function () {
			if ($.trim($input.val()) == '') {
				$input.css('color', options.inactiveColor);
				$input.val(defaultText);
			}
			if ($.trim($input.val()) == defaultText) {
				$input.css('color', options.inactiveColor);
			}
		});
		
		/**
		 * Try find parent form and clean default values on submit
		 */
		var parent = $input.parent();
		while (parent.attr('nodeName') && parent.attr('nodeName') != 'FORM') {
			parent = parent.parent();
		}
		if (parent.attr('nodeName') == 'FORM') {
			parent.submit(function () {
				if ($.trim($input.val()) == defaultText) {
					$input.val('');
				}
			})
		}
		
		// Return self
		return this;
	};		
})(jQuery);
